/*
 * ICG SRL - International Consulting Group 2011
 */
package org.heyma.core.hibernatedao;

import com.icg.entityclassutils.Attribute;
import com.icg.entityclassutils.DynamicEntityMap;
import com.icg.entityclassutils.Entity;
import java.io.Serializable;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.heyma.core.dao.Dao;
import org.heyma.db.utils.StatementUtil;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Johnston Castillo Valencia email: john.gnu@gmail.com
 * @since 01-03-2011
 */
@Repository
public class SpringHibernateDao extends HibernateDaoSupport implements Dao {

    @Autowired
    public SpringHibernateDao(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Transactional
    public void persist(Object entity) {
        getHibernateTemplate().saveOrUpdate(entity);
    }

    @Transactional
    public void persist(Object[] entities) {
        for (int i = 0; i < entities.length; i++) {
            persist(entities[i]);
        }
    }

    @Transactional(readOnly = true)
    public <T> List<T> find(Class<T> entityClass) {
        final List<T> entities = getHibernateTemplate().loadAll(entityClass);
        return entities;
    }

    @Transactional(readOnly = true)
    public <T> T load(Class<T> entityClass, Serializable id) {
        final T entity = (T) getHibernateTemplate().load(entityClass, id);
        return entity;
    }

    @Transactional(readOnly = true)
    public <T> T get(Class<T> entityClass, Serializable id) {
        final T entity = (T) getHibernateTemplate().get(entityClass, id);
        return entity;
    }

    @Transactional(readOnly = true)
    public <T> List<T> find(String hql) {
        final List<T> entities = getHibernateTemplate().find(hql);
        return entities;
    }

    @Transactional
    public void remove(Object entity) {
        getHibernateTemplate().delete(entity);
    }

    @Transactional
    public <T> List<T> pagination(Class<T> entityClass, int start, int limit) {
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(entityClass);
        criteria.setFirstResult(start);
        criteria.setMaxResults(limit);
        return criteria.list();
    }

    @Transactional
    public void fn() {
        Session sess = getHibernateTemplate().getSessionFactory().getCurrentSession();

    }

    @Transactional
    public void execute(String sql) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql);
        query.executeUpdate();
    }

    @Transactional
    public void execute(String sql, Long entity, Long chlid) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql);
        query.setParameter(0, entity);
        query.setParameter(1, chlid);
        query.executeUpdate();
    }

    @Transactional
    public boolean existTable(String schema, String tablename) {
        String st = String.format("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s' AND table_name='%s';", schema, tablename);

        List tables = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(st).list();
//        for (Object o : tables) {
//            if (o.toString().equals(name.toLowerCase())) {
//                System.out.println("Existe tabla: " + name);
//                return true;
//            }
//        }
//        System.out.println("No existe tabla: " + name);
        if (!tables.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    @Transactional
    public void persist(String SQL, Map<String, Object> values) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        String[] params = query.getNamedParameters();
        for (String param : params) {
            query.setParameter(param, values.get(param));
        }
        query.executeUpdate();
    }

    @Transactional(readOnly = true)
    public <T> List<T> selectSQL(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        return query.list();
    }

    @Transactional
    public <T> List<T> selectSQLRW(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        return query.list();
    }

//    @Transactional(readOnly = true)
//    public <T> List<T> selectSQLMapResult(String SQL) {
//        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
//        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
//        //List<Map<String, Object>> aliasToValueMapList = query.list();
//
//        return query.list();
//    }
    @Transactional
    public void selectSQLDelMapResult(String SQL, String id) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.executeUpdate();
    }

    @Transactional(readOnly = true)
    public <T> List<T> selectSQL(Class<T> javaClass, String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(Transformers.aliasToBean(javaClass));
        return query.list();
    }

    @Transactional(readOnly = true)
    public <T> T selectUniqueSQLResult(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        return (T) query.uniqueResult();
    }

    @Transactional
    public <T> List<T> selectSQL(String SQL, Map values) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        int i = 0;
        for (Object ob : values.entrySet()) {
            Entry en = (Entry) ob;
            query.setParameter(i, en.getValue());
            i++;
        }
        return query.list();
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> find(Entity entity, Long parent_id) {
        String SQL = StatementUtil.simpleSelectTable(entity, parent_id);
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        if (entity.getParent() != null) {
            query.setParameter(entity.getParent() + "_id", parent_id);
        }
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        for (Attribute a : entity.getAttributes()) {
            if (a.getType().equals(Attribute.ENTITY)) {
                if (a.getMultiplicity().equals(Attribute.MULTIPLICITY_1)) {
                }
            }
            if (a.getType().equals(Attribute.BOOLEAN)) {
            }
        }
        return aliasToValueMapList;
    }

    @Transactional(readOnly = true)
    public Map<String, Object> get(Entity entity, Long id) {
        String SQL = StatementUtil.simpleSelectTableRecord(entity);
        System.out.println(id);
        System.out.println(SQL);
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, id);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        Object object = query.uniqueResult();
        System.out.println(object.getClass());
        //query.list();
        return (HashMap<String, Object>) object;
    }

    @Transactional
    public void remove(String SQL, Long id) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, id);
        //AUDITORIA
        query.executeUpdate();
    }

    @Transactional
    public void update(String SQL, Map<String, String> tableMap, Map<String, Object> insertable) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        for (Entry<String, String> c : tableMap.entrySet()) {
            query.setParameter(c.getKey(), insertable.get(c.getKey()));
        }
        query.executeUpdate();
    }

    @Transactional
    public void persist(Entity entity, Map<String, Object> objectMap, Entity parent) throws ParseException {
        if (!this.existTable("public", entity.getId())) {
            this.execute(StatementUtil.getPGStatement(entity, null));
        }

//        String SQL = StatementUtil.getPersistSQLStatement(entity, DynamicEntityMap.SingleAttributesMap(objectMap));
//        System.out.println(SQL);
        //SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        Map<String, Object> insertable = DynamicEntityMap.parseEntityMap(objectMap, entity, parent);
//        for (Entry<String, Object> en : insertable.entrySet()) {
//            System.out.println(en.getKey() + " - " + en.getValue().getClass());
//        }
        String SQL = StatementUtil.getPersistSQLStatement(entity, parent, DynamicEntityMap.SingleAttributesMap(insertable));
        System.out.println(SQL);

        this.persist(SQL, insertable);

        /*
         * Si tiene multiples objetos adjuntos
         */
        if (DynamicEntityMap.hasMultipleAttributes(objectMap)) {
            Long eID = new Long(insertable.get(entity.primaryKeyName()).toString());
            Map<String, String[]> multiAttr = DynamicEntityMap.multipleAttributesMap(objectMap);
            for (Entry<String, String[]> m : multiAttr.entrySet()) {
                Attribute a = entity.getAttributeByName(m.getKey());

//                this.execute("DELETE FROM " + StatementUtil.relationName(a) + " WHERE " + entity.primaryKeyName() + "=" + eID);

                String subSQL = StatementUtil.getAttributePersistSQL(a);
                System.out.print(subSQL);
                for (String valID : m.getValue()) {
                    System.out.println(valID);
                    this.execute(subSQL, eID, new Long(valID));
                }
            }
        }
    }

    @Transactional
    public void persist(String SQL, Object[] values) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        int i = 0;
        for (Object o : values) {
            query.setParameter(i, o);
            i++;
        }
        query.executeUpdate();
    }

    @Transactional
    public Map<String, Object> getObjetPredio(Entity predio, long id) {
        String SQL = "SELECT predio_id,codigo,codigocatastral,manzana_id FROM public.predio WHERE predio_id = ?";
        System.out.println(id);
        System.out.println(SQL);
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, id);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        Object object = query.uniqueResult();
        System.out.println(object.getClass());
        //query.list();
        return (HashMap<String, Object>) object;
    }

    @Transactional
    public Map<String, Object> getObjetManzana(Entity manzana, long id) {
        String SQL = "SELECT manzana_id,codigo,sector_id,ST_AsText(the_geom) FROM public.manzana WHERE manzana_id = ?";//ST_AsText(the_geom)
        System.out.println(id);
        System.out.println(SQL);
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, id);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        Object object = query.uniqueResult();
        System.out.println(object.getClass());
        return (HashMap<String, Object>) object;
    }

    @Transactional
    public void update(String SQL, Map<String, Object> input) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        for (Entry<String, Object> entry : input.entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
        //query.setParameter(i, id);
        query.executeUpdate();

    }

    @Transactional
    public List<Map<String, Object>> it_construccion() {
        String SQL = "select construccion_id, predio_id, numero_bloque, uso_construccion,anio_construccion, sup_construida, sup_comun from construccion";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional
    public Map<String, Object> get(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return (HashMap<String, Object>) query.uniqueResult();
    }

    @Transactional
    public Map<String, Object> getObjectMap(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return (HashMap<String, Object>) query.uniqueResult();
    }

    //*********************Pamela***************************************
    @Transactional(readOnly = true)
    public Map<String, Object> getObjetFicha(long id) {
        String SQL = "SELECT * FROM public.fichacatastral WHERE fichacatastral_id = ?";
        System.out.println(id);
        System.out.println(SQL);
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, id);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return (HashMap<String, Object>) query.uniqueResult();
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getFichas(String predioid) {
        //String SQL = "SELECT * FROM public.fichacatastral where id_predio=" + predioid + " and (estado='Pendiente' or estado='Observado')";
        String SQL = "SELECT * FROM public.fichacatastral where id_predio=" + predioid + "";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

//    @Transactional
//    public List<Map<String, Object>> getFichasControl(String predioid) {
//        String SQL = "SELECT * FROM public.fichacatastral where id_predio=" + predioid + " and estado='Aprobado'";
//        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
//        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
//        List<Map<String, Object>> aliasToValueMapList = query.list();
//        return aliasToValueMapList;
//    }
    @Transactional
    public List<Map<String, Object>> getParametrosControl() {
        String SQL = "SELECT * FROM public.parametroscontrol ";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional
    public List<Map<String, Object>> getFiltroConsulta(String queryw, String criterio) {
        String SQL = "select * from parametroscontrol where fechaingreso='" + queryw + "'";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }
    //**************EJEMPLOS*************

    @Transactional
    public List<Map<String, Object>> getPersona() {
        String SQL = "SELECT * FROM public.ejemplopersona ";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional
    public List<Map<String, Object>> getZona() {
        String SQL = "SELECT * FROM public.ejemplozona ";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional
    public List<Map<String, Object>> getFiltroPersona(String queryw, String criterio) {
        String SQL = "select * from ejemplopersona where id_manzana=" + queryw;
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;

    }

    @Transactional
    public void metadata() {
//        String SQL = "selec * from ficha catastral;";
//        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
//        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
//        
//        getHibernateTemplate().getSessionFactory().getCurrentSession().
//        
//        
//        for (Iterator iter = configuration.getClassMappings(); iter.hasNext();) {
//            PersistentClass persistentClass = (PersistentClass) iter.next();
//            String table = persistentClass.getTable().getName();
//            // Code to truncate table (or just use a query with session.executeUpdate)
//        }
    }

    @Transactional
    public void update(Entity entity, Map<String, Object> objectMap, Entity parent) throws ParseException {
        //Map<String, Object> insertable = DynamicEntityMap.parseEntityMap(objectMap, entity);
        Map<String, Object> insertable = DynamicEntityMap.parseEntityMap(DynamicEntityMap.SingleAttributesMap(objectMap), entity, parent);
        String SQL = StatementUtil.getUpdateSQLStatement(entity);
        System.out.print("UPDATE: " + SQL);
        //SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);

        this.persist(SQL, insertable);


        /*
         * Si tiene multiples objetos adjuntos
         */
        if (DynamicEntityMap.hasMultipleAttributes(objectMap)) {
            Long eID = new Long(insertable.get(entity.primaryKeyName()).toString());
            Map<String, String[]> multiAttr = DynamicEntityMap.multipleAttributesMap(objectMap);
            for (Entry<String, String[]> m : multiAttr.entrySet()) {
                Attribute a = entity.getAttributeByName(m.getKey());

                this.execute("DELETE FROM " + StatementUtil.relationName(a) + " WHERE " + entity.primaryKeyName() + "=" + eID);

                String subSQL = StatementUtil.getAttributePersistSQL(a);
                System.out.print(subSQL);
                for (String valID : m.getValue()) {
                    System.out.println(valID);
                    this.execute(subSQL, eID, new Long(valID));
                }
            }
        }
    }

    @Transactional
    public List<Map<String, Object>> getPrediosMigrados() {
        String SQL = "SELECT * FROM public.predio ";
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> selectSQLMapResult(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional(readOnly = true)
    @Deprecated
    public List<Map<String, Object>> selectMapSQL(String SQL) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

//    @Transactional(readOnly = true)
//    public List<Map<String, Object>> selectSQLMapResult(String SQL, String param1) {
//        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
//        query.setParameter(0, param1);
//        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
//        List<Map<String, Object>> aliasToValueMapList = query.list();
//        return aliasToValueMapList;
//    }
    @Transactional(readOnly = true)
    public List<Map<String, Object>> selectSQLMapResult(String SQL, Map<String, Object> fields) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        for (Entry<String, Object> c : fields.entrySet()) {
            query.setParameter(c.getKey(), c.getValue());
        }
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        List<Map<String, Object>> aliasToValueMapList = query.list();
        return aliasToValueMapList;
    }

    @Transactional
    public void persist(String SQL, Map<String, String> table, Map<String, Object> insertable) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        System.out.println("setting parameters");
        for (Entry<String, String> c : table.entrySet()) {
            System.out.println(insertable.get(c.getValue()));
            query.setParameter(c.getKey(), insertable.get(c.getKey()));
        }
        query.executeUpdate();
    }

    @Transactional(readOnly = true)
    public Map<String, Object> get(String SQL, Object ID) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, ID);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return (HashMap<String, Object>) query.uniqueResult();
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> find(String SQL, Map<String, Object> params) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        for (Entry<String, Object> p : params.entrySet()) {
            query.setParameter(p.getKey(), p.getValue());
        }
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return query.list();
    }

    @Transactional
    public void sql_p(String SQL, Long id) {
    }

    @Transactional(readOnly = true)
    public boolean existObject(String schema, String entity, String field, Object value) {
        String SQL = StatementUtil.getCountByFieldFilterStatement(schema, entity, field);
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(field, value);
        Integer count = new Integer(query.uniqueResult().toString());
        System.out.println("exist object count: " + count);
        return count > 0 ? true : false;
    }

    @Transactional(readOnly = true)
    public boolean existColumn(String schema, String table, String column) {
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(StatementUtil.getInfExistColumn(schema, table, column));
        Integer count = new Integer(query.uniqueResult().toString());
        return count > 0 ? true : false;
    }

    public void insertActovity(String entity, Object id) {
        this.insertActovity("public", entity, id);
    }

    public void updateActovity(String entity, Object id) {
        this.updateActovity("public", entity, id);
    }

    public void deleteActovity(String entity, Object id) {
        this.deleteActovity("public", entity, id);
    }

    public void insertActovity(String schema, String entity, Object id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateActovity(String schema, String entity, Object id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void deleteActovity(String schema, String entity, Object id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Transactional
    public void delete(String schema, String entity, Object id) {
        String SQL = StatementUtil.deleteObject(schema, entity, entity+"_id");
        SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL);
        query.setParameter(0, id);
        query.executeUpdate();
    }
}
